<template>
  <el-form :model="form" ref="form" :rules="formRules" class="FPForm">
    <el-form-item
      prop="phoneNumber"
      label="手机号"
      :label-width="formLabelWidth"
    >
      <el-input
        v-model="form.phoneNumber"
        autocomplete="off"
        disabled
      ></el-input>
    </el-form-item>

    <el-form-item
      prop="newPassword"
      label="新密码"
      :label-width="formLabelWidth"
      :rules="[{ required: true, message: '新密码不能为空', trigger: 'blur' }]"
    >
      <el-input
        type="password"
        v-model="form.newPassword"
        autocomplete="off"
      ></el-input>
    </el-form-item>

    <el-form-item
      prop="ConfirmPassword"
      label="确认密码"
      :label-width="formLabelWidth"
    >
      <el-input
        type="password"
        v-model="form.ConfirmPassword"
        autocomplete="off"
      ></el-input>
    </el-form-item>

    <el-form-item
      prop="phoneCode"
      label="验证码"
      :label-width="formLabelWidth"
      :rules="[{ required: true, message: '验证码不能为空', trigger: 'blur' }]"
    >
      <el-input
        class="VerificationCode"
        v-model="form.phoneCode"
        autocomplete="off"
      ></el-input>
      <!-- <el-button :loading="register" type="success" class="register">{{
            send
          }}</el-button> -->

      <el-button
        :loading="register"
        type="success"
        class="register"
        v-show="verShow"
        @click="handleClick()"
        >点击获取验证码</el-button
      >
      <el-button
        :loading="register"
        type="success"
        class="register"
        v-show="!verShow"
        ><span>{{ timer }}</span
        >秒后重新获取</el-button
      >
    </el-form-item>

    <el-form-item class="dialog-footer">
      <el-button
        style="margin: 0 auto"
        type="primary"
        @click="dialogFormVisible"
        >确 定</el-button
      >
    </el-form-item>
  </el-form>
  <!-- <div slot="footer" class="dialog-footer">
    <el-button type="primary" @click="dialogFormVisible = false"
      >确 定</el-button>
  </div> -->
</template>

<script>
export default {
  props: {
    phone: {
      type: String,
      default: '请输入手机号',
    },
  },
  data() {
    const passwordConfirmLvalidate = (rule, value, callback) => {
      if (this.form.newPassword === '') {
        callback(new Error('请先输入新密码'))
      } else if (value !== this.form.newPassword) {
        callback(new Error('请保持密码与确认密码一致'))
      } else {
        callback()
      }
    }
    return {
      form: {
        phoneNumber: this.phone,
        newPassword: '',
        ConfirmPassword: '',
        phoneCode: '',
      },
      formRules: {
        ConfirmPassword: [
          {
            required: true,
            message: '确认密码不能为空',
            trigger: 'blur',
          },
          {
            required: true,
            trigger: 'blur',
            validator: passwordConfirmLvalidate,
          },
        ],
      },
      formLabelWidth: '120px',
      forgetformLabelWidth: '60px',
      register: false,
      send: '发送(60s)',
      verShow: true,
      timer: 60,
    }
  },
  methods: {
    handleClick() {
      this.verShow = false
      var auth_timer = setInterval(() => {
        this.timer--
        if (this.timer <= 0) {
          this.verShow = true
          clearInterval(auth_timer)
        }
      }, 1000)
    },
    dialogFormVisible() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.fullscreenLoading = true
          console.log(this.form)
        } else {
          this.$message({
            message: '请正确填写表单',
            type: 'warning',
          })
          return false
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.login-container {
  .el-form-item {
    background-color: #fff;

    .el-input {
      height: 49px;
      border: 1px solid #dcdfe6;
      border-radius: 4px;
    }

    .VerificationCode {
      width: 50%;
    }

    .register {
      margin-left: 30px;
      height: 47px;
    }
  }

  .dialog-footer {
    text-align: center;
  }
}
</style>
